//
//  DropBox.m
//  WebExtractSimpleViewer
//
//  Created by Daniel Braun on 10/03/07.
//  Copyright 2007 __MyCompanyName__. All rights reserved.
//

#import "DropBox.h"
#import "MyController.h"

@implementation DropBox

- (void) awakeFromNib 
{
	[self registerForDraggedTypes:[NSArray arrayWithObjects:
		NSFilenamesPboardType, nil]];
}

- (BOOL) validFilesForDrop:(NSArray *)files
{
	if ([files count] != 1) return NO;
	NSString *s=[files objectAtIndex:0];
	NSLog(@"got file %@\n", s);
	NSString *pe=[s pathExtension];
	if ([pe isEqualToString:@"webExtract"]) return YES;
	return NO;
}

- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender 
{
	NSPasteboard *pboard;
	NSDragOperation sourceDragMask;
	
	sourceDragMask = [sender draggingSourceOperationMask];
	pboard = [sender draggingPasteboard];
	
	if ( [[pboard types] containsObject:NSFilenamesPboardType] ) {
		if (sourceDragMask & NSDragOperationCopy) {
			NSArray *files = [pboard propertyListForType:NSFilenamesPboardType];
			if ([self validFilesForDrop:files]) return NSDragOperationCopy;
		}
	}
	return NSDragOperationNone;
}

- (void)draggingExited:(id <NSDraggingInfo>)sender 
{
	
}



- (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender {
	return YES;
}

- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender 
{
	NSPasteboard *pboard;
	NSDragOperation sourceDragMask;
	
	sourceDragMask = [sender draggingSourceOperationMask];
	pboard = [sender draggingPasteboard];
	
	if ( [[pboard types] containsObject:NSFilenamesPboardType] 
	     &&  (sourceDragMask & NSDragOperationCopy)) {
		NSArray *files = [pboard propertyListForType:NSFilenamesPboardType];
		if (![self validFilesForDrop:files]) return NO;
		return [controller dropFile:[files objectAtIndex:0]];
	}
	return NO;
}
- (void)concludeDragOperation:(id <NSDraggingInfo>)sender
{
}

@end
